//
//  BJDepositRateView.m
//  SZBank
//
//  Created by rimi on 16/7/22.
//  Copyright © 2016年 LuoYao. All rights reserved.
//

#import "BJDepositRateView.h"

@interface BJDepositRateView ()<ButtonAndTableViewDelegate>
{
    CGFloat frameWidth;/**< 自身宽度 */
    CGFloat frameHeight;/**< 自身高度 */
    CGFloat scrImageWidth;/**< 滚动视图宽度 */
    CGFloat scrImageHeight;/**< 滚动视图高度 */
    CGFloat imageWidth;/**< 计算图片的宽度 */
    CGFloat imageHeightl;/**< 计算图片的高度 */
    
    NSMutableArray *baseRateArray;/**< 基准数据 */
    NSMutableArray *realRaeArray;/**< 实际数据 */
    NSArray *monthArray;/**< 存入时间数组 */
}

@property (nonatomic)UIScrollView *scrollView;/**< 左边滚动视图 */
@property (nonatomic)UIImageView *caculationImageView;/**< 右边计算图片 */
@property (nonatomic)UIImageView *addImageView;/**< 广告图片 */
@property (nonatomic)UIImageView *scrollImageView;/**< 加载在滚动视图的图片 */
@property (nonatomic)ButtonAndTableView *timeButtonAndTabel;/**< 时间选择 */
@property (nonatomic)UIButton *caculationButton;/**< 计算button */
@property (nonatomic)UITextField *rateTextField;/**< 利率输入框 */
@property (nonatomic)UITextField *monthTextField;/**< 存入时间输入框 */
@property (nonatomic)UITextField *moneyTextField;/**< 存入金额 */
@property (nonatomic)UILabel *rateMoney;/**<利息 */
@property (nonatomic)UILabel *taxMoney;/**< 税 */
@property (nonatomic)UILabel *afterTaxMoney;/**< 税后 */
@end
@implementation BJDepositRateView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        //给宽高赋值
        frameWidth = self.frame.size.width;
        frameHeight = self.frame.size.height;
        scrImageWidth = self.scrollImageView.frame.size.width;
        scrImageHeight = self.scrollImageView.frame.size.height;
        //初始化数据源数组
        baseRateArray = [NSMutableArray array];
        realRaeArray = [NSMutableArray array];
        monthArray = @[@"",@"3",@"6",@"12",@"24",@"36",@"60",@"12",@"36",@"60",@"12",@"36",@"60",@"12",@"36",@"60",@"",@""];
//网络数据获取
        [NetWorkingDataManager getProtocolDepositRateDetailWithParam:@{@"id":@"402880c7401016cf0140101ed5920001"} success:^(id sender) {

            [baseRateArray addObject:sender[@"CURCURRENTINTERESTRATE"]];
            [baseRateArray addObject:sender[@"FIXEDFULLTHREEMONCUR"]];
            [baseRateArray addObject:sender[@"FIXEDFULLSIXMONCUR"]];
            [baseRateArray addObject:sender[@"FIXEDFULLONEYEARCUR"]];
            [baseRateArray addObject:sender[@"FIXEDFULLTWOYEARCUR"]];
            [baseRateArray addObject:sender[@"FIXEDFULLTHREEYEARCUR"]];
            [baseRateArray addObject:sender[@"FIXEDFULLFIVEYEARCUR"]];
            [baseRateArray addObject:@"2.85"];
            [baseRateArray addObject:sender[@"SMALLMONEYTHREEYEARCUR"]];
            [baseRateArray addObject:sender[@"SMALLMONEYFIVEYEARCUR"]];
            [baseRateArray addObject:sender[@"FIXEDCURCURRENT"]];
            [baseRateArray addObject:sender[@"AGREESAVINGCUR"]];
            [baseRateArray addObject:sender[@"MESMONEYONEDAYCUR"]];
            [baseRateArray addObject:sender[@"MESMONEYSEVENDAYCUR"]];
            [baseRateArray addObject:@"基准利率"];
           

            [realRaeArray addObject:sender[@"CURADJUSTEDINTERESTRATE"]];
            [realRaeArray addObject:sender[@"FIXEDFULLTHREEMONAFTER"]];
            [realRaeArray addObject:sender[@"FIXEDFULLSIXMONAFTER"]];
            [realRaeArray addObject:sender[@"FIXEDFULLONEYEARAFTER"]];
            [realRaeArray addObject:sender[@"FIXEDFULLTWOYEARAFTER"]];
            [realRaeArray addObject:sender[@"FIXEDFULLTHREEYEARAFTER"]];
            [realRaeArray addObject:@"5.225"];
            [realRaeArray addObject:sender[@"SMALLMONEYONEYEARAFTER"]];
            [realRaeArray addObject:sender[@"SMALLMONEYTHREEYEARAFTER"]];
            [realRaeArray addObject:sender[@"SMALLMONEYFIVEYEARAFTER"]];
            [realRaeArray addObject:sender[@"FIXEDCURAFTER"]];
            [realRaeArray addObject:sender[@"AGREESAVINGAFTER"]];
            [realRaeArray addObject:sender[@"MESMONEYONEDAYAFTER"]];
            [realRaeArray addObject:sender[@"MESMONEYSEVENDAYAFTER"]];
            [realRaeArray addObject:@"执行利率"];
            
            
            //添加label到滚动图片
            [self addLabelsToScrollImageViewWithArray:baseRateArray orginX:scrImageWidth * 0.3];
            self.caculationImageView.userInteractionEnabled =YES;
            [self addLabelsToScrollImageViewWithArray:realRaeArray orginX:scrImageWidth * 0.66];
            //添加视图到计算
            [self AddViewsTocalculateViewWithData:nil];
            [self.caculationImageView addSubview:self.rateMoney];
            [self.caculationImageView addSubview:self.taxMoney];
            [self.caculationImageView addSubview:self.afterTaxMoney];

        } failure:^(id sender) {
            NSLog(@"%@",sender);
        }];
        
        //为scrollView添加视图
        [self.scrollView addSubview:self.scrollImageView];
        [self.scrollView addSubview:self.addImageView];
        [self.scrollView addSubview:self.timeButtonAndTabel];
        //添加视图
        [self addSubview:self.scrollView];
        [self addSubview:self.caculationImageView];
        [self addSubview:self.caculationButton];
        
       
    }
    return self;
}

#pragma mark -- ButtonAndTableViewDelegate
- (void)btnTableView:(ButtonAndTableView *)btnTableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath selectTitle:(NSString *)selectTitle{
    self.monthTextField.text = monthArray[indexPath.row];
    if (indexPath.row < 7) {
        self.rateTextField.text = realRaeArray[indexPath.row];
    }
}

#pragma mark -- actions
- (void)addLabelsToScrollImageViewWithArray:(NSArray *)dataArray orginX:(CGFloat)orginX{

    for (int i = 0; i < 15; i ++) {
        UILabel *label = [[UILabel alloc]init];
    
        label.font = [UIFont systemFontOfSize:FONT_SIZE];
        label.textAlignment = NSTextAlignmentCenter;
         label.text = dataArray[i];

        if (i == 0) {
            label.frame = CGRectMake(orginX, scrImageHeight * 0.18, scrImageWidth * 0.3, scrImageHeight * 0.04);
        }else if (i > 0 && i < 7){
            label.frame = CGRectMake(orginX, scrImageHeight * 0.31 + scrImageHeight * 0.043 * (i - 1), scrImageWidth * 0.3, scrImageHeight * 0.04);
        }else if (i > 6 && i < 10){
            label.frame = CGRectMake(orginX, scrImageHeight * 0.61 + scrImageHeight * 0.043 * (i - 7), scrImageWidth * 0.3, scrImageHeight * 0.04);
        }else if (i == 10){
            label.frame = CGRectMake(orginX, scrImageHeight * 0.74, scrImageWidth * 0.3, scrImageHeight * 0.08);
            label.numberOfLines = 0;
        }else if (i == 11){
            label.frame = CGRectMake(orginX, scrImageHeight * 0.825, scrImageWidth * 0.3, scrImageHeight * 0.04);
        }else if (i == 14){
            label.frame = CGRectMake(orginX, scrImageHeight * 0.135,scrImageWidth * 0.3, scrImageHeight * 0.04 );
        }else{
            label.frame = CGRectMake(orginX, scrImageHeight * 0.915 + scrImageHeight * 0.043 * (i - 12), scrImageWidth * 0.3, scrImageHeight * 0.04);
        }
       
        [self.scrollImageView addSubview:label];
    }
}
- (void)action_buttonClicks:(UIButton *)sender{


    if ([self validateNumber:self.monthTextField.text] && [self validateNumber:self.moneyTextField.text]) {
        CGFloat money = [self.moneyTextField.text doubleValue];
        CGFloat rat = [self.rateTextField.text doubleValue];
        CGFloat month = [self.moneyTextField.text doubleValue];
        self.rateMoney.text = [NSString stringWithFormat:@"%.2f",((month / 12) * money * rat )/ 100];
        self.taxMoney.text = @"0.00";
        self.afterTaxMoney.text = [NSString stringWithFormat:@"%.2f",money + (rat / 12) * money * month / 100];
        
    }else{
        self.rateMoney.text = @"0.00";
        self.taxMoney.text = @"0.00";
        self.afterTaxMoney.text = @"0.00";
    }
}
/**
 *      正则表达式 判断字符串在 0-9之间
 */
- (BOOL)validateNumber:(NSString *) textString
{
    NSString* number=@"^[0-9]+$";
    NSPredicate *numberPre = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",number];
    return [numberPre evaluateWithObject:textString];
}


- (void)AddViewsTocalculateViewWithData:(NSArray *)dataArray{
    
    CGFloat calculateWidth = self.caculationImageView.frame.size.width;
    CGFloat calculateHeight = self.caculationImageView.frame.size.height;
    NSArray *array = @[@"活期",@"整存整取三个月",@"整存整取半年",@"整存整取一年",@"整存整取二年",@"整存整取三年",@"整存整取五年",@"零存整取一年",@"零存整取三年",@"零存整取五年",@"整存零取一年",@"整存零取三年",@"整存零取五年",@"存本取息一年",@"存本取息三年",@"存本取息五年",@"协定存款",@"通知存款一天",@"通知存款七天"];
    
    ButtonAndTableView  *selectionMoney= [[ButtonAndTableView alloc]initWithFrame:CGRectMake(calculateWidth * 0.45,calculateHeight * 0.1, calculateWidth * 0.5, calculateHeight * 0.06 ) dataSource:@{@"title":@"人民币",@"list":@[@"人民币"]} backImageName:@"银行业务_选择框" selectImageName:@"银行业务_选择框"];
   ButtonAndTableView  *selectionType = [[ButtonAndTableView alloc]initWithFrame:CGRectMake(calculateWidth * 0.45,CGRectGetMaxY(selectionMoney.frame) + calculateHeight * 0.02, calculateWidth * 0.5, calculateHeight * 0.06 ) dataSource:@{@"title":@"活期",@"list":array} backImageName:@"银行业务_选择框" selectImageName:@"银行业务_选择框"];
    selectionType.delegate = self;
    [self.caculationImageView addSubview:selectionMoney];
    [self.caculationImageView addSubview:selectionType];
    
    
    
    //添加textField
    for (int i = 0; i < 3; i ++) {
        //背景视图
        UIImageView *backGroundImage = [[UIImageView alloc]initWithFrame:CGRectMake(calculateWidth * 0.45 , calculateHeight * 0.28 + calculateHeight * 0.095 * i, calculateWidth * 0.4, calculateHeight * 0.08)];
        backGroundImage.image = [LocalDataManager getImageWithSourceOfPath:@"银行业务_输入框"];
        backGroundImage.userInteractionEnabled = YES;
        
        
        if (i == 0) {
            [backGroundImage addSubview:self.moneyTextField];
        }else if (i == 1){
             [backGroundImage addSubview:self.rateTextField];
        }else{
             [backGroundImage addSubview:self.monthTextField];
        }
        [self.caculationImageView addSubview:backGroundImage];
    }
}



#pragma mark -- getters
- (UIScrollView *)scrollView {
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, frameWidth * 0.6, frameHeight)];

        //偏移大小
        _scrollView.contentSize = CGSizeMake(self.scrollImageView.frame.size.width, self.scrollImageView.frame.size.height * 1.2);

    }
    return _scrollView;
}

- (UIImageView *)addImageView{
    if (!_addImageView) {
        _addImageView = ({
            UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0 , frameWidth * 0.6, frameHeight * 0.18)];
            imageView.image = [LocalDataManager getImageWithSourceOfPathForJPG:@"存款利率广告"];
            imageView;
        });
    }
    return _addImageView;
}

- (UIImageView *)caculationImageView{
    if (!_caculationImageView) {
        _caculationImageView = ({
            UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(self.addImageView.frame) + frameWidth * 0.01, frameHeight * 0.1 , frameWidth * 0.4, frameHeight * 0.7)];
            imageView.image = [LocalDataManager getImageWithSourceOfPath:@"银行业务_存款利率计算器"];
            _caculationImageView.userInteractionEnabled = YES;
            
            imageView;
        });

    }
    return _caculationImageView;
}


- (UIImageView *)scrollImageView{
    if (!_scrollImageView) {
        _scrollImageView = ({
            UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(self.addImageView.frame) + frameHeight * 0.02, self.scrollView.frame.size.width , KHEIGHT )];
            imageView.image = [LocalDataManager getImageWithSourceOfPath:@"银行业务_存款利率"];
            imageView.userInteractionEnabled = YES;
            imageView;
        });

    }
    return _scrollImageView;
}


- (ButtonAndTableView *)timeButtonAndTabel{
    if (!_timeButtonAndTabel) {
        _timeButtonAndTabel = [[ButtonAndTableView alloc]initWithFrame:CGRectMake(0,CGRectGetMaxY(self.addImageView.frame) + self.scrollView.frame.size.height * 0.06, self.scrollView.frame.size.width * 0.35, self.scrollView.frame.size.height * 0.06) dataSource:@{@"title":@"2012-07-06",@"list":@[@"2012-07-06"]} backImageName:@"银行业务_选择框" selectImageName:@"银行业务_选择框"];
    }
    return _timeButtonAndTabel;
}

- (UIButton *)caculationButton {
    if (!_caculationButton) {
        _caculationButton = [[UIButton alloc]initWithFrame:CGRectMake(CGRectGetMaxX(self.addImageView.frame) + frameWidth * 0.02, CGRectGetMaxY(self.caculationImageView.frame) + frameHeight * 0.02, frameWidth * 0.1, frameHeight * 0.05)];
        [_caculationButton setBackgroundImage:[LocalDataManager getImageWithSourceOfPath:@"银行业务_计算"] forState:UIControlStateNormal];
        [_caculationButton setTitle:@"计算" forState:UIControlStateNormal];
        [_caculationButton addTarget:self action:@selector(action_buttonClicks:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _caculationButton;
}

- (UITextField *)rateTextField {
    if (!_rateTextField) {
        _rateTextField = [[UITextField alloc]initWithFrame:CGRectMake( 0 ,0, frameWidth * 0.16, frameHeight * 0.055)];
        _rateTextField.text = @"0.38";
        _rateTextField.font = [UIFont systemFontOfSize:FONT_SIZE];
    }
    return _rateTextField;
}
- (UITextField *)monthTextField{
    if (!_monthTextField) {
        _monthTextField  = ({
        UITextField *textField  = [[UITextField alloc]initWithFrame:CGRectMake( 0 ,0, frameWidth * 0.16, frameHeight * 0.055)];
      
            textField.font = [UIFont systemFontOfSize:FONT_SIZE];
            
            textField;
        
        });
    }
    
    return _monthTextField;
}

- (UITextField *)moneyTextField{
    if (!_moneyTextField) {
        _moneyTextField = ({
            UITextField *textField  = [[UITextField alloc]initWithFrame:CGRectMake( 0 ,0, frameWidth * 0.16, frameHeight * 0.055)];
            
            textField.font = [UIFont systemFontOfSize:FONT_SIZE];
            
            textField;
            
        });

    }
    return _moneyTextField;
}
- (UILabel *)rateMoney {
    if (!_rateMoney) {
        _rateMoney = ({
            UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(self.caculationImageView.frame.size.width * 0.45,self.caculationImageView.frame.size.height * 0.568, self.caculationImageView.frame.size.width * 0.4, self.caculationImageView.frame.size.height * 0.08)];

           label.backgroundColor = [UIColor grayColor];
            label.font = [UIFont systemFontOfSize:FONT_SIZE];
            label.textAlignment = NSTextAlignmentCenter;
            
            label;
        });
    }
    return _rateMoney;
}
- (UILabel *)taxMoney {
    if (!_taxMoney) {
        _taxMoney = ({
           UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(self.caculationImageView.frame.size.width * 0.45,self.caculationImageView.frame.size.height * 0.665, self.caculationImageView.frame.size.width * 0.4, self.caculationImageView.frame.size.height * 0.08)];
           label.backgroundColor = [UIColor grayColor];
            label.font = [UIFont systemFontOfSize:FONT_SIZE];
            label.textAlignment = NSTextAlignmentCenter;
            
            label;
        });

    }
    return _taxMoney;
}

- (UILabel *)afterTaxMoney{
    if (!_afterTaxMoney) {
        _afterTaxMoney = ({
          UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(self.caculationImageView.frame.size.width * 0.45,self.caculationImageView.frame.size.height * 0.76, self.caculationImageView.frame.size.width * 0.4, self.caculationImageView.frame.size.height * 0.08)];
            
            label.backgroundColor = [UIColor grayColor];
            label.font = [UIFont systemFontOfSize:FONT_SIZE];
            label.textAlignment = NSTextAlignmentCenter;
          
            
            label;
        });

    }
    
    return _afterTaxMoney;
}
@end
